package com.heima.wemedia;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import okhttp3.*;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.*;

/*获取token*/
@SpringBootTest
class Sample {

    static final OkHttpClient HTTP_CLIENT = new OkHttpClient().newBuilder().build();

    public static void main(String []args) throws IOException{
        MediaType mediaType = MediaType.parse("application/json");
        RequestBody body = RequestBody.create(mediaType, "");
        Request request = new Request.Builder()
            .url("https://aip.baidubce.com/oauth/2.0/token?" +
                    "client_id=e1my6sEEGelkhqLPnaf07vtU&" +
                    "client_secret=WuPm3jGPW5yDqwz1OpNsF09UoyzFmK2V&grant_type=client_credentials")
            .method("POST", body)
            .addHeader("Content-Type", "application/json")
            .addHeader("Accept", "application/json")
            .build();
        Response response = HTTP_CLIENT.newCall(request).execute();
        JSONObject jsonObject = JSON.parseObject(response.body().string());
        Object access_token = jsonObject.get("access_token");
        System.out.println(access_token);
    }
}